// Package api
// @Author: zhangdi
package sysctrl

import (
	"server/application/constant/syscnt"
	"server/application/db/system/dbdto"
	dbsvc2 "server/application/db/system/dbsvc"
	"server/application/dto"
	"server/pkg/ginp"
	"server/pkg/utils"
	"server/pkg/where"
	"strconv"
	"strings"
)

func SysRoleCreate(ctx *ginp.ContextPro) {
	var params *dbdto.SysRoleCreate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	info, err := dbsvc2.SysRoleCreate(params)
	if err != nil {
		ctx.ErrJson("创建失败" + err.Error())
		return
	}
	ctx.OkJsonData(info)
}

func SysRoleUpdate(ctx *ginp.ContextPro) {
	var params *dbdto.SysRoleUpdate
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	wheres := where.Format(where.OptEqual("id", params.ID))
	err := dbsvc2.SysRoleUpdate(wheres, params)
	if err != nil {
		ctx.ErrJson("修改失败" + err.Error())
		return
	}
	ctx.OkJson()
}

func SysRoleDelete(ctx *ginp.ContextPro) {
	var params *dbdto.SysRoleDelete
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}

	err := dbsvc2.SysRoleDelete(where.FormatOne("id", "IN", params.ID))
	if err != nil {
		ctx.ErrJson("删除失败" + err.Error())
		return
	}
	ctx.OkJson()
	return
}

func SysRoleSearch(ctx *ginp.ContextPro) {
	var params *dto.ReqSearch
	if err := ctx.ShouldBindJSON(&params); err != nil {
		ctx.ErrJson("请求参数有误" + err.Error())
		return
	}
	list, total := dbsvc2.SysRoleSearch(params.Wheres, params.Extra)

	resp := &dto.RespSearch{
		List:     list,
		Total:    uint(total),
		PageNum:  uint(params.Extra.PageNum),
		PageSize: uint(params.Extra.PageSize),
	}
	ctx.OkJsonData(resp)
}

// GetRoleTreeList 获取角色树形列表
func GetRoleTreeList(ctx *ginp.ContextPro) {
	list, err := dbsvc2.SysRoleFindList(where.Format(), nil)
	if err != nil {
		return
	}

	_, total := dbsvc2.SysRoleSearch(where.Format(), nil)

	treeList := dbsvc2.BuildRoleMenuTree(list, 0)
	soreTreeList := dbsvc2.SortRoleTree(treeList)

	resp := &dto.RespSearch{
		List:     soreTreeList,
		Total:    uint(total),
		PageNum:  1,
		PageSize: 2000,
	}

	ctx.OkJsonData(resp)
}

// GetRoleTreeSelect 获取角色树形列表
func GetRoleTreeSelect(ctx *ginp.ContextPro) {
	list, err := dbsvc2.SysRoleFindList(where.Format(), nil)
	if err != nil {
		return
	}
	treeList := dbsvc2.BuildRoleSelectTree(list, 0)
	sortTreeList := dbsvc2.SortTreeSelect(treeList)

	//根据角色等级重新构造角色树
	userId := ctx.GetLoginUserId()
	if !dbsvc2.SysUserIsSuperAdmin(userId) {
		//获取当前用户的角色列表
		roleIds := dbsvc2.SysUserGetRoleIds(userId)
		//构造子角色树(不能看到所有的角色列表)
		subTree := dbsvc2.GetSysRoleSonTree(sortTreeList, roleIds)
		ctx.OkJsonData(subTree)
	} else {
		ctx.OkJsonData(sortTreeList)
	}

}

// SysRoleGetSuperRoleIds 获取超级管理员角色
func SysRoleGetSuperRoleIds() []uint {
	SuperRoleIds := strings.Split(syscnt.SuperRoleIds, ",")
	superRoleIdList := make([]uint, 0)
	for i := 0; i < len(SuperRoleIds); i++ {
		item, err := strconv.Atoi(SuperRoleIds[i])
		if err != nil {
			continue
		}
		superRoleIdList = append(superRoleIdList, uint(item))
	}
	return superRoleIdList
}

// SysRoleIsSuperRole 判断一个角色是否为超级角色
func SysRoleIsSuperRole(roleId uint) bool {
	superRoleIds := SysRoleGetSuperRoleIds()
	if utils.ArrContains(superRoleIds, roleId) {
		return true
	}

	return false
}
